#!/bin/bash

IMAGENAME=$1

. /usr/local/share/buildinfo/scripts/buildinfo_base.sh

set -x


if [ ! -z "$(get_version_cache_option)" ]; then

	#Delete the rsync package files
	if [[ ! ${IMAGENAME} =~ -slave- ]]; then
		/usr/bin/apt-get purge -y --auto-remove rsync
	fi
fi

apt-get clean -y
apt-get autoclean -y
apt-get autoremove -y --purge
rm -f /var/cache/apt/archives/*.deb /var/cache/apt/*.bin

if [[ ! ${IMAGENAME} =~ -slave- ]]; then
	rm -rf /var/lib/apt/lists/*
fi

rm -rf /sonic/target /ssh
rm -rf /tmp/*
rm -rf /debs /python-wheels  ~/.cache
find / | grep -E "__pycache__" | xargs rm -rf

rm -rf $BUILD_VERSION_PATH/*

# Disable the build hooks
symlink_build_hooks -d
set_reproducible_mirrors -d

# Remove the version deb preference
rm -f $VERSION_DEB_PREFERENCE
rm -f /etc/apt/preferences.d/01-versions-deb

# Remove all doc files
find /usr/share/doc -depth \( -type f -o -type l \) ! -name copyright | xargs rm || true

# Clean doc directories that are empty or only contain empty directories
while [ -n "$(find /usr/share/doc -depth -type d -empty -print -exec rmdir {} +)" ]; do :; done && \
    rm -rf               \
    /usr/share/man/*     \
    /usr/share/groff/*   \
    /usr/share/info/*    \
    /usr/share/lintian/* \
    /usr/share/linda/*   \
    /var/cache/man/*     \
    /usr/share/locale/*
